package tests_xml_parser;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import nbpservice.NbpServiceImpl.NBPXmlDocumentToExchangeRateParser;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import entities.ExchangeRate;

public class XmlParserTest
{
	Document document;
	SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
	final String good_document_path = "src\\tests_xml_parser\\kursy.xml";
	final String bad_date_document_path = "src\\tests_xml_parser\\bad_date_kursy.xml";
	final String bad_nodename_document_path = "src\\tests_xml_parser\\bad_nodename_kursy.xml";
	
	@Test
	public void getDateTest () throws FileNotFoundException, SAXException, IOException, ParserConfigurationException, ParseException
	{
		document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(good_document_path));
		Date dateFromNBPXmlDocument = NBPXmlDocumentToExchangeRateParser.getDateFromNBPXmlDocument(document);
		
		Date dateFromXmlFile = sdf.parse("01-12-2014");
		Assert.assertEquals(dateFromNBPXmlDocument, dateFromXmlFile);
	}
	
	@Test (expectedExceptions = java.text.ParseException.class) 
	public void should_throw_exception_with_wrong_date_format () throws FileNotFoundException, SAXException, IOException, ParserConfigurationException, ParseException
	{
		document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(bad_date_document_path));
		
		NBPXmlDocumentToExchangeRateParser.getDateFromNBPXmlDocument(document);
	}
	
	@Test
	public void badElementNameTest () throws FileNotFoundException, SAXException, IOException, ParserConfigurationException, ParseException
	{
		document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(bad_nodename_document_path));
		
		List<ExchangeRate> list = NBPXmlDocumentToExchangeRateParser.toList(document);
		
		Assert.assertEquals(list.size(), 0);
	}
	
	
}
